/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.services;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

public class ColorService {
	
	public static final int CUSTOM_COLOR_IDX =1;
	public static final int DEFAULT_COLOR_IDX =2;	
	
    private static Logger logger = Logger.getLogger(ColorService.class); 	
	
	private static final String[] names ={ "None", "Custom", "Black", "Blue", "Cyan", "Dark gray", 
										   "Gray", "Green", "Light gray", "Magenta",
										   "Orange", "Pink", "Red", "White", "Yellow" };

	private Color[] colors ={ null, Color.BLACK, Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY,
							  Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA,
							  Color.ORANGE, Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };   
	
	private ImageIcon[] images =new ImageIcon[ colors.length ];
	
	private static final Integer[] comboBoxIntArray =new Integer[ names.length ];
	
	private int imageWidth, imageHeight;
	
	public ColorService() {
		generateImages();
	}
	
	public String[] getNames() {
		return names;
	}
	
	public ImageIcon[] getImages() {
		return images;
	}
	
	public Color[] getColors() {
		return colors;
	}
	
	public Integer[] getComboBoxIntArray() {
		return comboBoxIntArray;
	}
	
	public int getIndexForColor( Color color ) {
		int colorIdx =-1;
		
		if ( color ==null ) {
			colorIdx =0;
		} else {
			for( int ct =2; ct < getColors().length; ct++ ) {
				// We ignore the alpha
				if ( color.getRed() == getColors()[ ct ].getRed() 
						&& color.getGreen() == getColors()[ ct ].getGreen() 
						&& color.getBlue() == getColors()[ ct ].getBlue() ) {
					colorIdx =ct;
					break;
				}
			}
			
			if ( colorIdx == -1 ) {
				colorIdx =ColorService.CUSTOM_COLOR_IDX;
			}
		}
		
		return colorIdx;
	}
	
	private void generateImages() {
		// Use texture service to get image size
		TextureService textureService =TextureService.getInstance();
		ImageIcon sample =textureService.getImageIcons()[1];
		imageWidth =sample.getIconWidth();
		imageHeight =sample.getIconHeight();
		
		for( int ct =0; ct < images.length; ct++ ) {
			comboBoxIntArray[ ct ] =new Integer( ct );
			
			images[ ct ] =buildImage( colors[ct] );	    	
		}

	}
	
	private ImageIcon buildImage( Color color ) {
		ImageIcon imageIcon =null;
		if ( color != null ) {
	    	BufferedImage bufferedImage =new BufferedImage( imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB );
	    	
	    	Graphics2D g = bufferedImage.createGraphics();
	
	    	Rectangle2D rect = new Rectangle2D.Double( 0, 0, imageWidth, imageHeight );
	    	g.setColor( Color.BLACK );
	    	g.draw( rect );
	    	g.setColor( color );
	    	g.fill( rect );
	    	
	    	imageIcon =new ImageIcon( bufferedImage );
		}
		
		return imageIcon;
	}
	
	public void setCustomColor( Color color ) {
		colors[ CUSTOM_COLOR_IDX ] =color;
		images[ CUSTOM_COLOR_IDX ] =buildImage( color );
	}
	
}
